package com.wctj.api.business.cmb.service.impl;

import com.wctj.api.base.response.Response;
import com.wctj.api.base.utils.JsonUtils;
import com.wctj.api.base.utils.OrderIdUtils;
import com.wctj.api.business.cmb.dto.*;
import com.wctj.api.business.common.dto.*;
import com.zgzg.shop.pay.business.cmb.dto.*;
import com.wctj.api.infra.cmb.DcHelper;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

import java.security.Security;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
* @Description: 招商银行 支付助手
* @Param:
* @return: 
* @Author: lydms
* @Date: 2024/9/26
*/

@Slf4j
@Component
public class CmbAssistant {
    private static final int BOUND_START = 1000000;
    private static final int BOUND_END = 9000000;
    private static Random random = new Random();

    /**
     * 封装 请求信息
     *
     * @param bodyObj 请求体
     * @return
     */
    public String GenerateRequestBody(JSONObject bodyObj, String funcode,String uid) throws JSONException {
        // 装载BC库,必须在应用的启动类中调用此函数
        Security.addProvider(new BouncyCastleProvider());
        System.setProperty("sun.net.http.retryPost", "false");

        // 准备接口数据，生产请替换为具体接口请求报文,包含所需的请求字段
        String currentDatetime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String reqid = new SimpleDateFormat("yyyyMMddHHmmssSSSS").format(new Date()) + (BOUND_START + random.nextInt(BOUND_END));

        JSONObject sendRequestObj = new JSONObject();
        JSONObject requestObj = new JSONObject();
        JSONObject headObj = new JSONObject();
        JSONObject signatureObj = new JSONObject();

        headObj.put("funcode", funcode); // 业务接口名，这里是查询业务模式接口，生产请替换为对应接口名
        headObj.put("userid", uid);
        headObj.put("reqid", reqid);

        /**
         * 对接招行请求报文先预添加signature内容，包含sigtim和sigdat两个KEY，sigtim为当前时间（年月日时分秒，24小时制），
         * 服务器验签时会和当前时间比较，如果前后相差一个小时会报错，sigdat先预填充"__signature_sigdat__"
         */
        signatureObj.put("sigdat", "__signature_sigdat__");
        signatureObj.put("sigtim", currentDatetime);

        requestObj.put("head", headObj);
        requestObj.put("body", bodyObj);

        sendRequestObj.put("request", requestObj);
        sendRequestObj.put("signature", signatureObj);
        return sendRequestObj.toString();
    }



    /**
     * 招行  代发其他  --- 提现
     * @param withdrawalSummaryDTO
     * @param payOrderNum
     * @return
     */
    public Response withdrawal(WithdrawalSummaryDTO withdrawalSummaryDTO, String payOrderNum, DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO) {
        try {

            // 业务接口名，这里是查询业务模式接口，生产请替换为对应接口名
            String funcode = "BB6BTHHL";
            JSONObject bodyObj = new JSONObject();
            JSONArray bb6busmod = new JSONArray();
            JSONObject aaa = new JSONObject();
            aaa.put("buscod", "N03020"); // N03010-代发工资       N03020-代发其他     N03040-钱包代发（从钱包出款，选择此业务时，结算通道赋值W）
            aaa.put("busmod", withdrawalSummaryDTO.getBusmod());  //S3019-无审批       S3015-经办
            bb6busmod.put(aaa);
            bodyObj.put("bb6busmod", bb6busmod);

            JSONArray bb6cdcbhx1 = new JSONArray();
            JSONObject bbb = new JSONObject();
            bbb.put("begtag", "Y");
            bbb.put("endtag", "Y");

            bbb.put("accnam", withdrawalSummaryDTO.getAccnam());
            bbb.put("accnbr", withdrawalSummaryDTO.getAccnbr());
            bbb.put("ttlamt", withdrawalSummaryDTO.getTtlamt());
            bbb.put("ttlcnt", withdrawalSummaryDTO.getTtlcnt());
            bbb.put("ttlnum", "1");

            bbb.put("curamt", withdrawalSummaryDTO.getTtlamt());
            bbb.put("curcnt", withdrawalSummaryDTO.getTtlcnt());
            bbb.put("ccynbr", "10");
            bbb.put("trstyp", "BYBK"); //BYBK        代发其他

            bbb.put("nusage", withdrawalSummaryDTO.getNusage());
            bbb.put("eptdat", new SimpleDateFormat("yyyyMMdd").format(new Date()));
            bbb.put("epttim", "000000");
            bbb.put("yurref", payOrderNum);
            bbb.put("chlflg", "M");
            bb6cdcbhx1.put(bbb);
            bodyObj.put("bb6cdcbhx1", bb6cdcbhx1);

            JSONArray bb6cdcdlx1 = new JSONArray();
            List<WithdrawalDetailDTO> withdrawalDetailList = withdrawalSummaryDTO.getWithdrawalDetailList();
            int i = 1;
            for (WithdrawalDetailDTO withdrawalDetailDTO : withdrawalDetailList) {
                JSONObject ccc = new JSONObject();
                ccc.put("trxseq", "0000" + i++);
                ccc.put("accnbr", withdrawalDetailDTO.getAccnbr());
                ccc.put("accnam", withdrawalDetailDTO.getAccnam());
                ccc.put("trsamt", withdrawalDetailDTO.getTrsAmt());
                ccc.put("eacbnk", withdrawalDetailDTO.getEacbnk());
                ccc.put("eaccty", withdrawalDetailDTO.getEaccty());
                bb6cdcdlx1.put(ccc);
            }
            bodyObj.put("bb6cdcdlx1", bb6cdcdlx1);


            DcHelper dchelper = new DcHelper(ddwxPaymentCfgInfoDTO.getSymUrl(), ddwxPaymentCfgInfoDTO.getUid(), ddwxPaymentCfgInfoDTO.getPrivateKey(), ddwxPaymentCfgInfoDTO.getPublicKey(), ddwxPaymentCfgInfoDTO.getSymKey());
            String resposeStr = dchelper.sendRequest(GenerateRequestBody(bodyObj, funcode,ddwxPaymentCfgInfoDTO.getUid()), funcode);
            JSONObject respose = JsonUtils.convertValue(resposeStr, JSONObject.class).getJSONObject("response");
            JSONObject head = respose.getJSONObject("head");
            if (!head.isNull("resultcode") && head.getString("resultcode").equals("SUC0000")) {
                return new Response().data(respose);
            } else {
                return new Response().failure(head.getString("resultmsg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("发起提现CMB异常", e);
            return new Response().failure(e.getMessage());
        }
    }

    /**
     * 单笔支付转账
     * @param cmbSingleTransferDTO
     * @param payOrderNum
     * @param ddwxPaymentCfgInfoDTO
     * @return
     */
    public Response SingleTransfer(CMBSingleTransferDTO cmbSingleTransferDTO, String payOrderNum, DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO) {
        try {
            // 业务接口名，这里是查询业务模式接口，生产请替换为对应接口名
            String funcode = "BB1PAYOP";

            JSONObject bodyObj = new JSONObject();
            JSONArray bb1paybmx1 = new JSONArray();
            JSONObject aaa = new JSONObject();
            aaa.put("busCod", "N02030");
            aaa.put("busMod", cmbSingleTransferDTO.getBusmod());
            bb1paybmx1.put(aaa);
            bodyObj.put("bb1paybmx1", bb1paybmx1);

            JSONArray bb1payopx1 = new JSONArray();
            JSONObject bbb = new JSONObject();
            bbb.put("ccyNbr", "10");
            bbb.put("crtAcc", cmbSingleTransferDTO.getCrtAcc());
            bbb.put("crtNam", cmbSingleTransferDTO.getCrtNam());
            bbb.put("crtBnk", cmbSingleTransferDTO.getCrtBnk());
            bbb.put("crtAdr", cmbSingleTransferDTO.getCrtAdr());
            bbb.put("brdNbr", "");
            bbb.put("dbtAcc", cmbSingleTransferDTO.getDbtAcc());
            bbb.put("nusAge", cmbSingleTransferDTO.getNusAge());
            bbb.put("bnkFlg", cmbSingleTransferDTO.getBnkFlg());
            bbb.put("trsAmt", cmbSingleTransferDTO.getTrsAmt());
            bbb.put("yurRef", payOrderNum);
            bb1payopx1.put(bbb);
            bodyObj.put("bb1payopx1", bb1payopx1);

            DcHelper dchelper = new DcHelper(ddwxPaymentCfgInfoDTO.getSymUrl(), ddwxPaymentCfgInfoDTO.getUid(), ddwxPaymentCfgInfoDTO.getPrivateKey(), ddwxPaymentCfgInfoDTO.getPublicKey(), ddwxPaymentCfgInfoDTO.getSymKey());
            String resposeStr = dchelper.sendRequest(GenerateRequestBody(bodyObj, funcode,ddwxPaymentCfgInfoDTO.getUid()), funcode);
            JSONObject respose = JsonUtils.convertValue(resposeStr, JSONObject.class).getJSONObject("response");
            JSONObject head = respose.getJSONObject("head");
            if (!head.isNull("resultcode") && head.getString("resultcode").equals("SUC0000")) {
                return new Response().data(respose);
            } else {
                return new Response().failure(head.getString("resultmsg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("单笔支付转账CMB异常", e);
            return new Response().failure(e.getMessage());
        }
    }

    /**
     * 批量支付转账
     * @param cmbBatchTransferDTO
     * @param payOrderNum
     * @param ddwxPaymentCfgInfoDTO
     * @return
     */
    public Response BatchTransfer(CMBBatchTransferDTO cmbBatchTransferDTO, String payOrderNum, DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO) {
        try {
            // 业务接口名，这里是查询业务模式接口，生产请替换为对应接口名
            String funcode = "BB1PAYBH";

            JSONObject bodyObj = new JSONObject();
            JSONArray bb1bmdbhx1 = new JSONArray();
            JSONObject aaa = new JSONObject();
            aaa.put("busCod", "N02030");
            aaa.put("busMod", cmbBatchTransferDTO.getBusmod());

            aaa.put("bthNbr", payOrderNum);
            aaa.put("dtlNbr", cmbBatchTransferDTO.getDtlNbr());
            aaa.put("ctnFlg", "N");
            aaa.put("ctnSts", "");
            bb1bmdbhx1.put(aaa);
            bodyObj.put("bb1bmdbhx1", bb1bmdbhx1);

            JSONArray bb1paybhx1 = new JSONArray();

            List<CMBBatchTransferDetailDTO> cmbBatchTransferDetailDTOList = cmbBatchTransferDTO.getCmbBatchTransferDetailDTOList();
            for (CMBBatchTransferDetailDTO ntqacinfx : cmbBatchTransferDetailDTOList) {
                JSONObject ccc = new JSONObject();
                ccc.put("ccyNbr", "10");
                ccc.put("crtAcc", ntqacinfx.getCrtAcc());
                ccc.put("crtNam", ntqacinfx.getCrtNam());
                ccc.put("dbtAcc", ntqacinfx.getDbtAcc());
                ccc.put("nusAge", ntqacinfx.getNusAge());
                ccc.put("bnkFlg", ntqacinfx.getBnkFlg());
                ccc.put("trsAmt", ntqacinfx.getTrsAmt());
                ccc.put("yurRef", OrderIdUtils.getOrderId());
                bb1paybhx1.put(ccc);
            }
            bodyObj.put("bb1paybhx1", bb1paybhx1);

            DcHelper dchelper = new DcHelper(ddwxPaymentCfgInfoDTO.getSymUrl(), ddwxPaymentCfgInfoDTO.getUid(), ddwxPaymentCfgInfoDTO.getPrivateKey(), ddwxPaymentCfgInfoDTO.getPublicKey(), ddwxPaymentCfgInfoDTO.getSymKey());
            String resposeStr = dchelper.sendRequest(GenerateRequestBody(bodyObj, funcode,ddwxPaymentCfgInfoDTO.getUid()), funcode);
            JSONObject respose = JsonUtils.convertValue(resposeStr, JSONObject.class).getJSONObject("response");
            JSONObject head = respose.getJSONObject("head");
            if (!head.isNull("resultcode") && head.getString("resultcode").equals("SUC0000")) {
                return new Response().data(respose);
            } else {
                return new Response().failure(head.getString("resultmsg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("批量支付转账CMB异常", e);
            return new Response().failure(e.getMessage());
        }
    }
    
    /**
     * 获取 账号详细信息
     * @param accountDTO
     * @param ddwxPaymentCfgInfoDTO
     * @return
     */
    public Response getAccountDetail(AccountDTO accountDTO,DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO) {
        try {
            // 业务接口名，这里是查询业务模式接口，生产请替换为对应接口名
            String funcode = "NTQACINF";
            JSONObject bodyObj = new JSONObject();
            JSONArray ntqacinfxAry = new JSONArray();
            JSONObject aaa = new JSONObject();
            aaa.put("accnbr", accountDTO.getAccnbr());
            ntqacinfxAry.put(aaa);
            bodyObj.put("ntqacinfx", ntqacinfxAry);

            //DcHelper dchelper = new DcHelper(cmbPayBean.getUrl(), cmbPayBean.getUid(), cmbPayBean.getPrivateKey(), cmbPayBean.getPublicKey(), cmbPayBean.getSymKey());
            DcHelper dchelper = new DcHelper(ddwxPaymentCfgInfoDTO.getSymUrl(), ddwxPaymentCfgInfoDTO.getUid(), ddwxPaymentCfgInfoDTO.getPrivateKey(), ddwxPaymentCfgInfoDTO.getPublicKey(), ddwxPaymentCfgInfoDTO.getSymKey());
            String resposeStr = dchelper.sendRequest(GenerateRequestBody(bodyObj, funcode,ddwxPaymentCfgInfoDTO.getUid()), funcode);
            JSONObject respose = JsonUtils.convertValue(resposeStr, JSONObject.class).getJSONObject("response");
            JSONObject head = respose.getJSONObject("head");
            if (!head.isNull("resultcode") && head.getString("resultcode").equals("SUC0000")) {
                return new Response().data(respose);
            } else {
                return new Response().failure(head.getString("resultmsg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("getAccountDetail error", e);
            return new Response().failure(e.getMessage());
        }
    }

    /**
     * 查询交易信息列表
     * @param transactionDTO
     * @param ddwxPaymentCfgInfoDTO
     * @return
     */
    public Response getTransactionList(TransactionDTO transactionDTO, DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO) {
        try {
            // 业务接口名，这里是查询业务模式接口，生产请替换为对应接口名
            String funcode = "trsQryByBreakPoint";

            JSONObject bodyObj = new JSONObject();
            JSONArray TRANSQUERYBYBREAKPOINT_X1 = new JSONArray();
            JSONObject aaa = new JSONObject();
            aaa.put("cardNbr", transactionDTO.getAccnbr());
            aaa.put("beginDate", transactionDTO.getBeginDate());
            aaa.put("endDate", transactionDTO.getEndDate());
            aaa.put("transactionSequence", "1"); //仅在不传入续传键值时有效，表示初始查询从第几笔开始查询，默认从第1笔开始查询
            aaa.put("queryAcctNbr", "");
            TRANSQUERYBYBREAKPOINT_X1.put(aaa);
            bodyObj.put("TRANSQUERYBYBREAKPOINT_X1", TRANSQUERYBYBREAKPOINT_X1);

            DcHelper dchelper = new DcHelper(ddwxPaymentCfgInfoDTO.getSymUrl(), ddwxPaymentCfgInfoDTO.getUid(), ddwxPaymentCfgInfoDTO.getPrivateKey(), ddwxPaymentCfgInfoDTO.getPublicKey(), ddwxPaymentCfgInfoDTO.getSymKey());
            String resposeStr = dchelper.sendRequest(GenerateRequestBody(bodyObj, funcode,ddwxPaymentCfgInfoDTO.getUid()), funcode);
            JSONObject respose = JsonUtils.convertValue(resposeStr, JSONObject.class).getJSONObject("response");
            JSONObject head = respose.getJSONObject("head");
            if (!head.isNull("resultcode") && head.getString("resultcode").equals("SUC0000")) {
                return new Response().data(respose);
            } else {
                return new Response().failure(head.getString("resultmsg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("getTransactionList error", e);
            return new Response().failure(e.getMessage());
        }
    }

    public Response getElectronReceipt(ElectronReceiptDTO electronReceiptDTO, DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO) {
        try {
            // 业务接口名，这里是查询业务模式接口，生产请替换为对应接口名
            String funcode = "ASYCALHD";

            JSONObject bodyObj = new JSONObject();
            bodyObj.put("primod", "PDF");
            bodyObj.put("eacnbr", electronReceiptDTO.getAccnbr());
            bodyObj.put("begdat", electronReceiptDTO.getBeginDate());
            bodyObj.put("enddat", electronReceiptDTO.getEndDate());
            bodyObj.put("rrcflg", "");
            bodyObj.put("begamt", "");
            bodyObj.put("endamt", "");
            bodyObj.put("rrccod", "");

            //DcHelper dchelper = new DcHelper(cmbPayBean.getUrl(), cmbPayBean.getUid(), cmbPayBean.getPrivateKey(), cmbPayBean.getPublicKey(), cmbPayBean.getSymKey());
            DcHelper dchelper = new DcHelper(ddwxPaymentCfgInfoDTO.getSymUrl(), ddwxPaymentCfgInfoDTO.getUid(), ddwxPaymentCfgInfoDTO.getPrivateKey(), ddwxPaymentCfgInfoDTO.getPublicKey(), ddwxPaymentCfgInfoDTO.getSymKey());
            String resposeStr = dchelper.sendRequest(GenerateRequestBody(bodyObj, funcode,ddwxPaymentCfgInfoDTO.getUid()), funcode);
            JSONObject respose = JsonUtils.convertValue(resposeStr, JSONObject.class).getJSONObject("response");
            JSONObject head = respose.getJSONObject("head");
            if (!head.isNull("resultcode") && head.getString("resultcode").equals("SUC0000")) {
                return new Response().data(respose);
            } else {
                return new Response().failure(head.getString("resultmsg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("getElectronReceipt error", e);
            return new Response().failure(e.getMessage());
        }
    }

    public Response getElectronReceiptResult(ElectronReceiptResultDTO electronReceiptResultDTO, DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO) {
        try {
            // 业务接口名，这里是查询业务模式接口，生产请替换为对应接口名
            String funcode = "DCTASKID";

            JSONObject bodyObj = new JSONObject();
            bodyObj.put("taskid", electronReceiptResultDTO.getTaskid());
            bodyObj.put("qwenab", "");

            //DcHelper dchelper = new DcHelper(cmbPayBean.getUrl(), cmbPayBean.getUid(), cmbPayBean.getPrivateKey(), cmbPayBean.getPublicKey(), cmbPayBean.getSymKey());
            DcHelper dchelper = new DcHelper(ddwxPaymentCfgInfoDTO.getSymUrl(), ddwxPaymentCfgInfoDTO.getUid(), ddwxPaymentCfgInfoDTO.getPrivateKey(), ddwxPaymentCfgInfoDTO.getPublicKey(), ddwxPaymentCfgInfoDTO.getSymKey());
            String resposeStr = dchelper.sendRequest(GenerateRequestBody(bodyObj, funcode,ddwxPaymentCfgInfoDTO.getUid()), funcode);
            JSONObject respose = JsonUtils.convertValue(resposeStr, JSONObject.class).getJSONObject("response");
            JSONObject head = respose.getJSONObject("head");
            if (!head.isNull("resultcode") && head.getString("resultcode").equals("SUC0000")) {
                return new Response().data(respose);
            } else {
                return new Response().failure(head.getString("resultmsg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("getElectronReceiptResult error", e);
            return new Response().failure(e.getMessage());
        }
    }

    /**
     *代发批次查询
     * @param releasePayBatchDTO
     * @param ddwxPaymentCfgInfoDTO
     * @return
     */
    public Response getReleasePayBatch(ReleasePayBatchDTO releasePayBatchDTO, DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO) {
        try {
            // 业务接口名，这里是查询业务模式接口，生产请替换为对应接口名
            // 业务接口名，这里是查询业务模式接口，生产请替换为对应接口名
            String funcode = "BB6BTHQY";

            JSONObject bodyObj = new JSONObject();
            JSONArray bb6bthqyy1 = new JSONArray();
            JSONObject aaa = new JSONObject();
            aaa.put("buscod", "N03020"); //N03010-代发工资    N03020-代发    N03040-钱包代发
            aaa.put("yurref", "");
            aaa.put("bgndat", releasePayBatchDTO.getBgndat());
            aaa.put("enddat", releasePayBatchDTO.getEnddat());
            aaa.put("dattyp", "A"); //A：经办日期；  B：期望日期；默认为A
            aaa.put("cntkey", "");
            aaa.put("rsv50z", "");
            bb6bthqyy1.put(aaa);
            bodyObj.put("bb6bthqyy1", bb6bthqyy1);

            DcHelper dchelper = new DcHelper(ddwxPaymentCfgInfoDTO.getSymUrl(), ddwxPaymentCfgInfoDTO.getUid(), ddwxPaymentCfgInfoDTO.getPrivateKey(), ddwxPaymentCfgInfoDTO.getPublicKey(), ddwxPaymentCfgInfoDTO.getSymKey());
            String resposeStr = dchelper.sendRequest(GenerateRequestBody(bodyObj, funcode,ddwxPaymentCfgInfoDTO.getUid()), funcode);
            JSONObject respose = JsonUtils.convertValue(resposeStr, JSONObject.class).getJSONObject("response");
            JSONObject head = respose.getJSONObject("head");
            if (!head.isNull("resultcode") && head.getString("resultcode").equals("SUC0000")) {
                return new Response().data(respose);
            } else {
                return new Response().failure(head.getString("resultmsg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("getElectronReceiptResult error", e);
            return new Response().failure(e.getMessage());
        }
    }

    /**
     * 代发明细查询
     * @param releasePayDetailDTO
     * @param ddwxPaymentCfgInfoDTO
     * @return
     */
    public Response getReleasePayDetail(ReleasePayDetailDTO releasePayDetailDTO, DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO) {
        try {
            // 业务接口名，这里是查询业务模式接口，生产请替换为对应接口名
            String funcode = "BB6DTLQY";

            JSONObject bodyObj = new JSONObject();
            JSONArray bb6dtlqyy1 = new JSONArray();
            JSONObject aaa = new JSONObject();
            aaa.put("reqnbr", releasePayDetailDTO.getReqnbr());
            aaa.put("bthnbr", "");
            aaa.put("trxseq", "");
            aaa.put("histag", "");
            aaa.put("rsv50z", "");
            bb6dtlqyy1.put(aaa);
            bodyObj.put("bb6dtlqyy1", bb6dtlqyy1);

            DcHelper dchelper = new DcHelper(ddwxPaymentCfgInfoDTO.getSymUrl(), ddwxPaymentCfgInfoDTO.getUid(), ddwxPaymentCfgInfoDTO.getPrivateKey(), ddwxPaymentCfgInfoDTO.getPublicKey(), ddwxPaymentCfgInfoDTO.getSymKey());
            String resposeStr = dchelper.sendRequest(GenerateRequestBody(bodyObj, funcode,ddwxPaymentCfgInfoDTO.getUid()), funcode);
            JSONObject respose = JsonUtils.convertValue(resposeStr, JSONObject.class).getJSONObject("response");
            JSONObject head = respose.getJSONObject("head");
            if (!head.isNull("resultcode") && head.getString("resultcode").equals("SUC0000")) {
                return new Response().data(respose);
            } else {
                return new Response().failure(head.getString("resultmsg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("getElectronReceiptResult error", e);
            return new Response().failure(e.getMessage());
        }
    }

    /**
     * 代发明细对账单查询请求
     * @param releasePayReconciliationDTO
     * @param ddwxPaymentCfgInfoDTO
     * @return
     */
    public Response getReleasePayReconciliation(ReleasePayReconciliationDTO releasePayReconciliationDTO, DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO) {
        try {
            // 业务接口名，这里是查询业务模式接口，生产请替换为对应接口名
            String funcode = "DCAGPPDF";

            JSONObject bodyObj = new JSONObject();
            bodyObj.put("payeac", releasePayReconciliationDTO.getPayeac());
            bodyObj.put("begdat", releasePayReconciliationDTO.getBegdat());
            bodyObj.put("enddat", releasePayReconciliationDTO.getEnddat());
            bodyObj.put("buscod", "N03020"); //代发工资:N03010,其他：N03020
            bodyObj.put("busmod", releasePayReconciliationDTO.getBusmod());  //S3019-无审批       S3015-经办 //对应业务类型的业务模式，查询的账号需要在该业务模式的可经办账号列表里面
            bodyObj.put("eacnam", "");
            bodyObj.put("eacnbr", "");
            bodyObj.put("ptyref", "");
            bodyObj.put("trxsrl", "");
            bodyObj.put("minamt", "");
            bodyObj.put("maxamt", "");
            bodyObj.put("prtmod", "S"); //S：逐笔打印：每笔明细生成一个PDF文件      M：批量打印：所有明细生成生成到一个PDF文件里面
            bodyObj.put("begidx", "0");
            bodyObj.put("pagsiz", ""); //默认1000，最大限制1000，打印模式为M时表示一个文件里面包含的明细的条数，打印模式为S时表示一次可以查询明细的条数。
            bodyObj.put("cntkey", ""); //首次查询为空，当返回报文里面有cntkey返回时表明需要续传，请将返回报文中的cntkey值放入该字段继续请求

            DcHelper dchelper = new DcHelper(ddwxPaymentCfgInfoDTO.getSymUrl(), ddwxPaymentCfgInfoDTO.getUid(), ddwxPaymentCfgInfoDTO.getPrivateKey(), ddwxPaymentCfgInfoDTO.getPublicKey(), ddwxPaymentCfgInfoDTO.getSymKey());
            String resposeStr = dchelper.sendRequest(GenerateRequestBody(bodyObj, funcode,ddwxPaymentCfgInfoDTO.getUid()), funcode);
            JSONObject respose = JsonUtils.convertValue(resposeStr, JSONObject.class).getJSONObject("response");
            JSONObject head = respose.getJSONObject("head");
            if (!head.isNull("resultcode") && head.getString("resultcode").equals("SUC0000")) {
                return new Response().data(respose);
            } else {
                return new Response().failure(head.getString("resultmsg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("getElectronReceiptResult error", e);
            return new Response().failure(e.getMessage());
        }
    }

    public Response getReleasePayReconciliationResult(ReleasePayReconciliationResultDTO releasePayReconciliationResultDTO, DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO) {
        try {
            // 业务接口名，这里是查询业务模式接口，生产请替换为对应接口名
            String funcode = "DCTASKID";

            JSONObject bodyObj = new JSONObject();
            bodyObj.put("taskid", releasePayReconciliationResultDTO.getTaskid());
            bodyObj.put("qwenab", "");

            DcHelper dchelper = new DcHelper(ddwxPaymentCfgInfoDTO.getSymUrl(), ddwxPaymentCfgInfoDTO.getUid(), ddwxPaymentCfgInfoDTO.getPrivateKey(), ddwxPaymentCfgInfoDTO.getPublicKey(), ddwxPaymentCfgInfoDTO.getSymKey());
            String resposeStr = dchelper.sendRequest(GenerateRequestBody(bodyObj, funcode,ddwxPaymentCfgInfoDTO.getUid()), funcode);
            JSONObject respose = JsonUtils.convertValue(resposeStr, JSONObject.class).getJSONObject("response");
            JSONObject head = respose.getJSONObject("head");
            if (!head.isNull("resultcode") && head.getString("resultcode").equals("SUC0000")) {
                return new Response().data(respose);
            } else {
                return new Response().failure(head.getString("resultmsg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("getElectronReceiptResult error", e);
            return new Response().failure(e.getMessage());
        }
    }

    public Response getEBankReconciliationPending(EBankReconciliationPendingDTO eBankReconciliationPendingDTO, DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO) {
        try {
            // 业务接口名，这里是查询业务模式接口，生产请替换为对应接口名
            // 业务接口名，这里是查询业务模式接口，生产请替换为对应接口名
            String funcode = "NTNSTQRY1";

            JSONObject bodyObj = new JSONObject();
            JSONArray ntnstqryx = new JSONArray();
            JSONObject aaa = new JSONObject();
            aaa.put("eacnbr", eBankReconciliationPendingDTO.getEacnbr());
            aaa.put("busmod", eBankReconciliationPendingDTO.getBusmod());
            aaa.put("yermth", eBankReconciliationPendingDTO.getYermth());
            aaa.put("endmth", eBankReconciliationPendingDTO.getEndmth());
            aaa.put("cntflg", "");
            ntnstqryx.put(aaa);
            bodyObj.put("ntnstqryx", ntnstqryx);

            DcHelper dchelper = new DcHelper(ddwxPaymentCfgInfoDTO.getSymUrl(), ddwxPaymentCfgInfoDTO.getUid(), ddwxPaymentCfgInfoDTO.getPrivateKey(), ddwxPaymentCfgInfoDTO.getPublicKey(), ddwxPaymentCfgInfoDTO.getSymKey());
            String resposeStr = dchelper.sendRequest(GenerateRequestBody(bodyObj, funcode,ddwxPaymentCfgInfoDTO.getUid()), funcode);
            JSONObject respose = JsonUtils.convertValue(resposeStr, JSONObject.class).getJSONObject("response");
            JSONObject head = respose.getJSONObject("head");
            if (!head.isNull("resultcode") && head.getString("resultcode").equals("SUC0000")) {
                return new Response().data(respose);
            } else {
                return new Response().failure(head.getString("resultmsg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("getElectronReceiptResult error", e);
            return new Response().failure(e.getMessage());
        }
    }

    //todo
    public Response getEBankReconciliationHandling(EBankReconciliationHandlingDTO eBankReconciliationHandlingDTO, DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO) {
        try {
            // 业务接口名，这里是查询业务模式接口，生产请替换为对应接口名
            String funcode = "NTNSTOPR";

            JSONObject bodyObj = new JSONObject();
            bodyObj.put("qwenab", "");

            DcHelper dchelper = new DcHelper(ddwxPaymentCfgInfoDTO.getSymUrl(), ddwxPaymentCfgInfoDTO.getUid(), ddwxPaymentCfgInfoDTO.getPrivateKey(), ddwxPaymentCfgInfoDTO.getPublicKey(), ddwxPaymentCfgInfoDTO.getSymKey());
            String resposeStr = dchelper.sendRequest(GenerateRequestBody(bodyObj, funcode,ddwxPaymentCfgInfoDTO.getUid()), funcode);
            JSONObject respose = JsonUtils.convertValue(resposeStr, JSONObject.class).getJSONObject("response");
            JSONObject head = respose.getJSONObject("head");
            if (!head.isNull("resultcode") && head.getString("resultcode").equals("SUC0000")) {
                return new Response().data(respose);
            } else {
                return new Response().failure(head.getString("resultmsg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("getElectronReceiptResult error", e);
            return new Response().failure(e.getMessage());
        }
    }

    public Response getEBankReconciliationBusiness(EBankReconciliationBusinessDTO eBankReconciliationBusinessDTO, DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO) {
        try {
            // 业务接口名，这里是查询业务模式接口，生产请替换为对应接口名
            // 业务接口名，这里是查询业务模式接口，生产请替换为对应接口名
            String funcode = "NTNSTLST";

            JSONObject bodyObj = new JSONObject();
            JSONArray ntnstwacy = new JSONArray();
            JSONObject aaa = new JSONObject();
            aaa.put("bgndat", eBankReconciliationBusinessDTO.getBgndat());
            aaa.put("enddat", eBankReconciliationBusinessDTO.getEnddat());
            aaa.put("yermth", eBankReconciliationBusinessDTO.getYermth());
            aaa.put("oprlgn", eBankReconciliationBusinessDTO.getOprlgn());
            aaa.put("autstr", "");
            aaa.put("rtnstr", "");
            aaa.put("ctnkey", "");
            ntnstwacy.put(aaa);
            bodyObj.put("ntnstwacy", ntnstwacy);

            DcHelper dchelper = new DcHelper(ddwxPaymentCfgInfoDTO.getSymUrl(), ddwxPaymentCfgInfoDTO.getUid(), ddwxPaymentCfgInfoDTO.getPrivateKey(), ddwxPaymentCfgInfoDTO.getPublicKey(), ddwxPaymentCfgInfoDTO.getSymKey());
            String resposeStr = dchelper.sendRequest(GenerateRequestBody(bodyObj, funcode,ddwxPaymentCfgInfoDTO.getUid()), funcode);
            JSONObject respose = JsonUtils.convertValue(resposeStr, JSONObject.class).getJSONObject("response");
            JSONObject head = respose.getJSONObject("head");
            if (!head.isNull("resultcode") && head.getString("resultcode").equals("SUC0000")) {
                return new Response().data(respose);
            } else {
                return new Response().failure(head.getString("resultmsg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("getElectronReceiptResult error", e);
            return new Response().failure(e.getMessage());
        }
    }

    public Response getEBankReconciliationDetail(EBankReconciliationDetailDTO eBankReconciliationDetailDTO, DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO) {
        try {
            // 业务接口名，这里是查询业务模式接口，生产请替换为对应接口名
            String funcode = "NTNSTQRY2";

            JSONObject bodyObj = new JSONObject();
            JSONArray ntnstqryx = new JSONArray();
            JSONObject aaa = new JSONObject();
            aaa.put("reqnbr", eBankReconciliationDetailDTO.getReqnbr());
            aaa.put("eacnbr", eBankReconciliationDetailDTO.getEacnbr());
            aaa.put("yermth", eBankReconciliationDetailDTO.getYermth());
            aaa.put("endmth", eBankReconciliationDetailDTO.getEndmth());
            aaa.put("cntflg", "");
            ntnstqryx.put(aaa);
            bodyObj.put("ntnstqryx", ntnstqryx);

            DcHelper dchelper = new DcHelper(ddwxPaymentCfgInfoDTO.getSymUrl(), ddwxPaymentCfgInfoDTO.getUid(), ddwxPaymentCfgInfoDTO.getPrivateKey(), ddwxPaymentCfgInfoDTO.getPublicKey(), ddwxPaymentCfgInfoDTO.getSymKey());
            String resposeStr = dchelper.sendRequest(GenerateRequestBody(bodyObj, funcode,ddwxPaymentCfgInfoDTO.getUid()), funcode);
            JSONObject respose = JsonUtils.convertValue(resposeStr, JSONObject.class).getJSONObject("response");
            JSONObject head = respose.getJSONObject("head");
            if (!head.isNull("resultcode") && head.getString("resultcode").equals("SUC0000")) {
                return new Response().data(respose);
            } else {
                return new Response().failure(head.getString("resultmsg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("getElectronReceiptResult error", e);
            return new Response().failure(e.getMessage());
        }
    }


    /**
     * 企银批量支付批次查询
     * @param paymentBatchDTO
     * @param ddwxPaymentCfgInfoDTO
     * @return
     */
    public Response getPaymentBatch(PaymentBatchDTO paymentBatchDTO, DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO) {
        try {
            // 业务接口名，这里是查询业务模式接口，生产请替换为对应接口名
            String funcode = "BB1QRYBT";

            JSONObject bodyObj = new JSONObject();
            JSONArray bb1qrybtx1 = new JSONArray();
            JSONObject aaa = new JSONObject();
            aaa.put("begDat", paymentBatchDTO.getBegDat());
            aaa.put("endDat", paymentBatchDTO.getEndDat());
            aaa.put("autStr", "");
            aaa.put("rtnStr", "");
            bb1qrybtx1.put(aaa);
            bodyObj.put("bb1qrybtx1", bb1qrybtx1);

            DcHelper dchelper = new DcHelper(ddwxPaymentCfgInfoDTO.getSymUrl(), ddwxPaymentCfgInfoDTO.getUid(), ddwxPaymentCfgInfoDTO.getPrivateKey(), ddwxPaymentCfgInfoDTO.getPublicKey(), ddwxPaymentCfgInfoDTO.getSymKey());
            String resposeStr = dchelper.sendRequest(GenerateRequestBody(bodyObj, funcode,ddwxPaymentCfgInfoDTO.getUid()), funcode);
            JSONObject respose = JsonUtils.convertValue(resposeStr, JSONObject.class).getJSONObject("response");
            JSONObject head = respose.getJSONObject("head");
            if (!head.isNull("resultcode") && head.getString("resultcode").equals("SUC0000")) {
                return new Response().data(respose);
            } else {
                return new Response().failure(head.getString("resultmsg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("getElectronReceiptResult error", e);
            return new Response().failure(e.getMessage());
        }
    }


    /**
     * 企银批量支付明细查询
     * @param paymentDetailDTO
     * @param ddwxPaymentCfgInfoDTO
     * @return
     */
    public Response getPaymentDetail(PaymentDetailDTO paymentDetailDTO, DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO) {
        try {
            // 业务接口名，这里是查询业务模式接口，生产请替换为对应接口名
            String funcode = "BB1QRYBD";

            JSONObject bodyObj = new JSONObject();
            JSONArray bb1qrybdy1 = new JSONArray();
            JSONObject aaa = new JSONObject();
            aaa.put("bthNbr", paymentDetailDTO.getBthNbr());
            aaa.put("autStr", "");
            aaa.put("rtnStr", "");
            aaa.put("ctnKey", "");
            bb1qrybdy1.put(aaa);
            bodyObj.put("bb1qrybdy1", bb1qrybdy1);

            DcHelper dchelper = new DcHelper(ddwxPaymentCfgInfoDTO.getSymUrl(), ddwxPaymentCfgInfoDTO.getUid(), ddwxPaymentCfgInfoDTO.getPrivateKey(), ddwxPaymentCfgInfoDTO.getPublicKey(), ddwxPaymentCfgInfoDTO.getSymKey());
            String resposeStr = dchelper.sendRequest(GenerateRequestBody(bodyObj, funcode,ddwxPaymentCfgInfoDTO.getUid()), funcode);
            JSONObject respose = JsonUtils.convertValue(resposeStr, JSONObject.class).getJSONObject("response");
            JSONObject head = respose.getJSONObject("head");
            if (!head.isNull("resultcode") && head.getString("resultcode").equals("SUC0000")) {
                return new Response().data(respose);
            } else {
                return new Response().failure(head.getString("resultmsg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("getElectronReceiptResult error", e);
            return new Response().failure(e.getMessage());
        }
    }

}
